package com.chtw.oasystem.service.impl;

import com.chtw.oasystem.bean.*;
import com.chtw.oasystem.common.changelogs.ChangeLogsUtils;
import com.chtw.oasystem.mapper.ApproveLogMapper;
import com.chtw.oasystem.mapper.ApproveMapper;
import com.chtw.oasystem.mapper.UsersMapper;
import com.chtw.oasystem.service.ApproveService;
import com.chtw.oasystem.utils.Session;
import com.chtw.oasystem.utils.TimeUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author chtw
 * @date 2020-11-25 23:12
 */
@Service
public class ApproveServiceImpl implements ApproveService {

    @Autowired
    private ApproveMapper approveMapper;

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private ApproveLogMapper approveLogMapper;

    @Autowired
    private ChangeLogServiceImpl changeLogService;

    @Autowired
    private FileUploadServiceImpl fileUploadService;

    @Override
    public boolean create(HttpSession session, int recordsId, String name) {
        try {
            FullUserInfo currentUser = Session.getCurrentUser(session);
            Approve approve = fileUploadService.getPeopleApprove(currentUser.getUserName(), recordsId);
            if (approve.getStatus() == 1) {
                throw new RuntimeException("当前文件正在审批中");
            }
            UsersExample example = new UsersExample();
            UsersExample.Criteria re = example.createCriteria();
            re.andUserIdEqualTo(currentUser.getLeaderId());
            Users leaderUser = usersMapper.selectByExample(example).get(0);
            approve = new Approve();
            approve.setCreated(TimeUtils.formatDate(new Date(), null));
            approve.setCurrentUser(leaderUser.getUserName());
            approve.setRecordsId(recordsId);
            approve.setStatus(1);
            approve.setApproveName(name);
            approve.setSubmitter(currentUser.getUserName());
            changeLogService.saveChangeLog(ChangeLogsUtils.generateApproveChangeLog(currentUser.getUserName(), name, "提交"));
            return approveMapper.insertSelective(approve) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

    }

    @Override
    public boolean approve(HttpSession session, int id, String status, String desc) {
        try {
            FullUserInfo currentUser = Session.getCurrentUser(session);
            if (currentUser.getRoleId() > 3) {
                throw new RuntimeException("没有权限");
            }
            Approve approve = approveMapper.selectByPrimaryKey(id);
            approve.setCurrentUser(currentUser.getUserName());
            ApproveLog log = new ApproveLog();
            log.setApproveId(id);
            log.setApproveName(currentUser.getUserName());
            log.setStatus(status);
            log.setDescription(desc);
            approveLogMapper.insert(log);

            if (currentUser.getRoleId() == 2) {
                approve.setCurrentUser(currentUser.getUserName());
                if ("审批通过".equals(status)) {
                    // 0 审批通过 1 审批中 2 审批未通过
                    approve.setStatus(0);
                } else {
                    approve.setStatus(2);
                }
                return approveMapper.updateByPrimaryKeySelective(approve) > 0;
            } else if (currentUser.getRoleId() < 1) {
                approve.setCurrentUser(currentUser.getUserName());
                if ("审批通过".equals(status)) approve.setStatus(0);
                else approve.setStatus(2);
                return approveMapper.updateByPrimaryKeySelective(approve) > 0;
            } else if (currentUser.getRoleId() == 3)  {
                UsersExample example = new UsersExample();
                UsersExample.Criteria re = example.createCriteria();
                re.andUserIdEqualTo(currentUser.getLeaderId());
                Users leader = usersMapper.selectByExample(example).get(0);
                approve.setCurrentUser(leader.getUserName());
                if ("审批通过".equals(status)) approve.setStatus(0);
                else approve.setStatus(2);
                return approveMapper.updateByPrimaryKeySelective(approve) > 0;
            }else{
                throw new RuntimeException("没有权限");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

    }

    @Override
    public PageInfo<Approve> list(HttpSession session, int page, int limit) {
        try {
            FullUserInfo currentUser = Session.getCurrentUser(session);

            PageHelper.startPage(page, limit);
            ApproveExample example = new ApproveExample();
            ApproveExample.Criteria re = example.createCriteria();
            if (currentUser.getRoleId() > 1) {
                example.or().andSubmitterEqualTo(currentUser.getUserName());
                example.or().andCurrentUserEqualTo(currentUser.getUserName());
            }
            if (currentUser.getRoleId() > 3) {
                re.andSubmitterEqualTo(currentUser.getUserName());
            }
            List<Approve> approves = approveMapper.selectByExample(example);
            return new PageInfo<>(approves);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}
